#include<bits/stdc++.h>
using namespace std;
#define int long long
struct node {
	int x, i;

} a[100005];
bool cmp(node n1, node n2)
{
	return n1.x < n2.x;
}
signed main()
{
	int n, m;
	cin >> n >> m;
	for (int i = 1; i <= n; i++) {
//		cin >> a[i];
		cin >> a[i].x;
		a[i].i = i;
	}
	sort(a + 1, a + n + 1, cmp);
	int res  = INT_MIN;
	if (n == 2 * m) {
//		int a1, a2, b1, b2;
		vector<int> v1(m, 0), v2(m, 0);
		int cnt = 0;
		for (int i = 1; i <= n; i++) {
			int temp = a[i].i % m;
			if (v1[temp] == 0) {
				v1[temp] = a[i].x;
				cnt ++;
			}
			if (cnt == m) {
				break;
			}
		}
		for (int i = n; i >= 1; i--) {
			int temp = a[i].i % m;
			if (v2[temp] == 0) {
				v2[temp] = a[i].x;
				cnt ++;
			}
			if (cnt == m) {
				break;
			}
		}
		for(int i=0;i<m;i++){
			res = max(res,v2[i] - v1[i]);
		}

	}
	cout << res;
	return 0;
}
